<?php

class DepositController extends BaseController
{

	/**
	 * 保障金管理
	 */
	public function actionAdmin()
	{
		$model=new Company('search');
		$model->unsetAttributes();  // clear any default values
		$model->zc_deposit_status = 2;//保障金审核通过的商家
		if(isset($_GET['Company']))
			$model->attributes=$_GET['Company'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	
	/**
	 * 商家保障金记录
	 */
	public function actionDeposit($id)
	{
		$model=new CmpDeposit('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = $id;
		
		//店铺信息
		$company = Company::model()->findByPk($id);
		if(isset($_GET['CmpDeposit']))
			$model->attributes=$_GET['CmpDeposit'];
	
		$this->render('deposit',array(
				'model'=>$model,
				'company'=>$company
		));
	}
	
	/**
	 * 保障金提现
	 */
	public function actionPayout($id){
		//实例化商家信息
		$company = Company::model()->findByPk($id);
		if($company===null)
			throw new CHttpException(404,'The requested page does not exist.');
		
		$model = new CmpDeposit();
		
		//ajax数据验证
		$this->performAjaxValidation($model);
		
		if(isset($_POST['CmpDeposit'])){
			$model->attributes = $_POST['CmpDeposit'];
			$model->zcd_amount_log = $company->zc_deposit - $model->zcd_amount;
			$model->zm_id = $company->zm_id;
			$model->zcd_type = 2;//类型:1充值2.扣除
			$model->zcd_event = "管理员提现";
			if($model->save()){
				//更新商家保障金余额
				$company->zc_deposit = $model->zcd_amount_log;
				if($company->save()){
					//发送邮件
					if($_POST['email_notice']==1){
						$message = "你的保障金被管理员提现：".$model->zcd_amount."元<br>理由是：".$model->zcd_note;
						EmailTemplates::model()->sentMail($model->zm_id,9,array('message'=>$message));
					}
					//成功跳转
					$this->redirect(array('admin'));
				}else{
					//失败跳转
					$this->render('../index/error',array('message'=>'对不起，保障金提取操作失败，请稍后再试！'));
					exit;
				}
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，保障金提取操作失败，请稍后再试！'));
				exit;
			}
		}
		
		$this->render('deposit_payout',array(
				'company'=>$company,
				'model'=>$model,
		));
	}

	/**
	 * 保障金充值
	 */
	public function actionPayin($id){
		//实例化商家信息
		$company = Company::model()->findByPk($id);
		if($company===null)
			throw new CHttpException(404,'The requested page does not exist.');
	
		$model = new CmpDeposit();
	
		//ajax数据验证
		$this->performAjaxValidation($model);
	
		if(isset($_POST['CmpDeposit'])){
			$model->attributes = $_POST['CmpDeposit'];
			$model->zcd_amount_log = $company->zc_deposit + $model->zcd_amount;
			$model->zm_id = $company->zm_id;
			$model->zcd_type = 1;//类型:1充值2.扣除
			$model->zcd_event = "管理员充值";
			if($model->save()){
				//更新商家保障金余额
				$company->zc_deposit = $model->zcd_amount_log;
				if($company->save()){
					//发送邮件
					if($_POST['email_notice']==1){
						$message = "你的保障金被管理员充值：".$model->zcd_amount."元<br>理由是：".$model->zcd_note;
						EmailTemplates::model()->sentMail($model->zm_id,9,array('message'=>$message));
					}
					//成功跳转
					$this->redirect(array('admin'));
				}else{
					//失败跳转
					$this->render('../index/error',array('message'=>'对不起，保障金充值操作失败，请稍后再试！'));
					exit;
				}
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，保障金充值操作失败，请稍后再试！'));
				exit;
			}
		}
	
		$this->render('deposit_payin',array(
				'company'=>$company,
				'model'=>$model,
		));
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=CmpDeposit::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-deposit-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 导出列表，各商家保障金列表
	 */
	public function actionExport(){
		$model = new Company('search');
		$model->unsetAttributes();  // clear any default values
		$model->zc_deposit_status = 2;//保障金审核通过的商家
		$companys = $model->search();
		$this->toExcel($companys,array('zc_name','zc_deposit'),'商家保障金统计');
	}
	
	/**
	 * 导出列表,商家保障金变动明细
	 */
	public function actionExportDeposit($id){
		$model=new CmpDeposit('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = $id;
		$data = $model->search();
		$company = Company::model()->findByPk($id);
		$this->toExcel($data,'',$company->zc_name.'网铺保障金明细');
	}
	
	public function behaviors()
	{
		return array(
				'eexcelview'=>array(
						'class'=>'ext.eexcelview.EExcelBehavior',
				),
		);
	}
}
